sources <- c("ESPN", "FantasyData", "FantasyPros", "FantasySharks", 
             "Yahoo", "FantasyFootballNerd", "NumberFire",
             "RTSports", "Walterfootball")

scrape <- scrape_data(src = sources,
                      pos = c('QB', 'RB', 'WR', 'TE', 'DST'),
                      season = 2020, 
                      week = week)

Simulation Time!

n_sims <- 10000

tic()
sim_lu <- map_df(1:n_sims, generate_lineup) %>%
  rename(pts_base = points) %>%  
  mutate(position = factor(position, 
                           levels = c("QB", "RB", "WR", "TE", "DST"))) %>% 
  select(lineup, Name, team, position, pts_base, pts_pred, sd_pts, Salary)
toc()
## 305.662 sec elapsed

Results

ggplotly(sim_lu %>% 
           group_by(Name, position, Salary) %>% 
           dplyr::summarize(lu = n_distinct(lineup)) %>% 
           ungroup() %>% 
           group_by(position) %>% 
           top_n(10, lu) %>% 
           ungroup() %>% 
           arrange(position, desc(lu)) %>% 
           mutate(Name = factor(Name),
                  Name = fct_reorder(Name, lu),
                  pct = round(lu / n_sims, 3) * 100) %>% 
           ggplot(aes(x = Name, y = pct, fill = Salary,
                      text = paste(Name, "in", lu, "lineups with", Salary, "salary"))) +
           geom_bar(stat = "identity") +
           facet_wrap(~position, ncol = 2, scales = "free_y") +
           coord_flip() +
           scale_fill_viridis_c() +
           xlab("") +
           ylab("Lineups (thousands)") +
           ggtitle("Top 10 Players By Position")) %>% 
  ggplotly(tooltip = "text")
plyr_lu <- sim_lu %>%
  group_by(Name, position) %>%
  dplyr::summarize(lu=n_distinct(lineup)) %>%
  ungroup() 

ggplotly(projections %>% 
  filter(avg_type=='weighted') %>%
  mutate(Name = ifelse(pos=="DST", last_name, paste(first_name, last_name))) %>%
  inner_join(fan_duel, by = c("Name", "position")) %>%
  select(Name, team, position, points, Salary, sd_pts) %>%
  left_join(plyr_lu, by='Name') %>%
  replace_na(list(lu=0)) %>%
  mutate(lu_bin=ifelse(lu==0, '0 Lineups', '>=1 Lineups'),
         lu_5=cut(lu,5, labels = FALSE)) %>%
  ggplot(aes(x=Salary, y=points, color=lu_bin, size=sd_pts, text=Name)) +
  geom_point() +
  theme_minimal() +
  scale_color_manual(values = c('red', 'blue'), name="") +
  geom_smooth(inherit.aes = FALSE, aes(x=Salary, y=points), method = 'lm') +
  ylab('Projected Points') +
  xlab('Salary') +
  ggtitle('Who makes it into Optimized Lineups?') +
  scale_x_continuous(labels=scales::dollar))
lu_df <- sim_lu %>%
  group_by(lineup) %>%
  dplyr::summarize(lineup_pts=sum(pts_pred),
                   lineup_sd=sum(sd_pts)) %>%
  ungroup()

pto <- psel(lu_df, low(lineup_sd) * high(lineup_pts))


ggplotly(ggplot(lu_df, aes(y=lineup_pts, x=lineup_sd, text = paste0("Lineup: ", lineup))) +
  geom_point() +
  geom_point(data=pto, size=5) +
  ylab('Lineup Points') +
  xlab('Lineup Points St Dev') +
  ggtitle('Lineup Points vs. Uncertainty',
          subtitle = 'Pareto Lineups Bolded'))
psel(lu_df, low(lineup_sd) * high(lineup_pts)) %>%
  left_join(sim_lu, by='lineup') %>%
  group_by(lineup) %>%
  arrange(lineup_pts, position, desc(Salary)) %>%
  select(lineup, lineup_pts, lineup_sd, Name, team, position, pts_pred, sd_pts, Salary) %>%
  mutate_at(vars(lineup_pts, lineup_sd, pts_pred, sd_pts), function(x) round(x, 2)) %>%
  knitr::kable() %>%
  kable_styling(fixed_thead = T) %>%
  column_spec(1:3, bold=TRUE) %>%
  collapse_rows(columns = 1:3, valign = 'top') %>%
  scroll_box(height = '700px', width = '100%')
lineup lineup_pts lineup_sd Name team position pts_pred sd_pts Salary
398 130.88 2.55 Russell Wilson SEA QB 24.35 0.67 8700
Kareem Hunt CLE RB 18.90 0.18 8200
Jamaal Williams GBP RB 15.96 0.15 7000
Giovani Bernard CIN RB 14.41 0.26 5900
Stefon Diggs BUF WR 14.00 0.28 7100
Tyler Boyd CIN WR 12.67 0.06 6400
Tre’Quan Smith NOS WR 9.65 0.16 5300
George Kittle SFO TE 14.59 0.76 7700
Seattle Seahawks SEA DST 6.35 0.03 3700
5097 131.56 3.10 Russell Wilson SEA QB 24.14 0.67 8700
Kareem Hunt CLE RB 18.81 0.18 8200
Jonathan Taylor IND RB 16.65 0.36 7300
Giovani Bernard CIN RB 14.47 0.26 5900
Stefon Diggs BUF WR 13.87 0.28 7100
Tyler Boyd CIN WR 12.64 0.06 6400
Brandon Aiyuk SFO WR 12.70 0.41 5900
Travis Kelce KCC TE 14.33 0.22 7900
New York Jets NYJ DST 3.95 0.67 2500
6396 133.02 3.18 Russell Wilson SEA QB 25.52 0.67 8700
Kareem Hunt CLE RB 18.86 0.18 8200
Jamaal Williams GBP RB 15.62 0.15 7000
Giovani Bernard CIN RB 13.97 0.26 5900
Keenan Allen LAC WR 15.55 0.66 7400
Tyler Boyd CIN WR 12.62 0.06 6400
Brandon Aiyuk SFO WR 12.35 0.41 5900
Darren Waller LVR TE 12.17 0.77 6800
Seattle Seahawks SEA DST 6.36 0.03 3700
1874 135.02 3.60 Lamar Jackson BAL QB 25.36 0.91 8300
Kareem Hunt CLE RB 19.09 0.18 8200
Jamaal Williams GBP RB 15.86 0.15 7000
Giovani Bernard CIN RB 14.36 0.26 5900
Stefon Diggs BUF WR 13.89 0.28 7100
Justin Jefferson MIN WR 14.75 0.87 6600
Tyler Boyd CIN WR 12.68 0.06 6400
Travis Kelce KCC TE 13.93 0.22 7900
New York Jets NYJ DST 5.11 0.67 2500
2563 135.15 3.69 Russell Wilson SEA QB 26.10 0.67 8700
Kareem Hunt CLE RB 18.81 0.18 8200
Jamaal Williams GBP RB 15.93 0.15 7000
Giovani Bernard CIN RB 14.67 0.26 5900
Keenan Allen LAC WR 15.04 0.66 7400
Stefon Diggs BUF WR 13.94 0.28 7100
Tyler Boyd CIN WR 12.70 0.06 6400
Darren Waller LVR TE 12.83 0.77 6800
New York Jets NYJ DST 5.12 0.67 2500
2343 135.27 3.76 Russell Wilson SEA QB 25.61 0.67 8700
Kareem Hunt CLE RB 18.65 0.18 8200
Jamaal Williams GBP RB 15.85 0.15 7000
Giovani Bernard CIN RB 14.11 0.26 5900
Tyler Lockett SEA WR 16.63 1.24 7200
Tyler Boyd CIN WR 12.69 0.06 6400
Brandon Aiyuk SFO WR 11.86 0.41 5900
Darren Waller LVR TE 13.43 0.77 6800
Seattle Seahawks SEA DST 6.43 0.03 3700
8719 135.36 3.81 Russell Wilson SEA QB 24.38 0.67 8700
Kareem Hunt CLE RB 19.14 0.18 8200
Jamaal Williams GBP RB 15.95 0.15 7000
Giovani Bernard CIN RB 14.33 0.26 5900
Justin Jefferson MIN WR 13.23 0.87 6600
Tyler Boyd CIN WR 12.71 0.06 6400
Brandon Aiyuk SFO WR 12.49 0.41 5900
George Kittle SFO TE 15.50 0.76 7700
New Orleans Saints NOS DST 7.64 0.44 3600
2605 135.59 4.12 Russell Wilson SEA QB 24.89 0.67 8700
Kareem Hunt CLE RB 18.77 0.18 8200
Jamaal Williams GBP RB 15.91 0.15 7000
Giovani Bernard CIN RB 14.17 0.26 5900
Kenny Golladay DET WR 16.54 0.97 7600
Tyler Boyd CIN WR 12.67 0.06 6400
Brandon Aiyuk SFO WR 12.46 0.41 5900
George Kittle SFO TE 14.94 0.76 7700
New York Jets NYJ DST 5.23 0.67 2500
1589 136.68 4.28 Russell Wilson SEA QB 24.80 0.67 8700
Derrick Henry TEN RB 20.12 0.14 9500
Kareem Hunt CLE RB 18.70 0.18 8200
Giovani Bernard CIN RB 14.72 0.26 5900
Tyler Lockett SEA WR 16.54 1.24 7200
Tyler Boyd CIN WR 12.62 0.06 6400
Brandon Aiyuk SFO WR 12.98 0.41 5900
Hunter Henry LAC TE 10.20 0.66 5600
New York Jets NYJ DST 6.01 0.67 2500
1189 137.05 4.48 Russell Wilson SEA QB 24.03 0.67 8700
Kareem Hunt CLE RB 18.60 0.18 8200
Jamaal Williams GBP RB 15.77 0.15 7000
Giovani Bernard CIN RB 14.24 0.26 5900
Tyler Lockett SEA WR 17.14 1.24 7200
Tyler Boyd CIN WR 12.68 0.06 6400
Brandon Aiyuk SFO WR 12.63 0.41 5900
Darren Waller LVR TE 13.52 0.77 6800
Denver Broncos DEN DST 8.46 0.75 3900
3852 137.76 4.60 Russell Wilson SEA QB 24.54 0.67 8700
Kareem Hunt CLE RB 18.88 0.18 8200
Jonathan Taylor IND RB 16.23 0.36 7300
Giovani Bernard CIN RB 14.22 0.26 5900
Tyler Lockett SEA WR 17.54 1.24 7200
Tyler Boyd CIN WR 12.66 0.06 6400
Brandon Aiyuk SFO WR 12.19 0.41 5900
George Kittle SFO TE 16.19 0.76 7700
New York Jets NYJ DST 5.30 0.67 2500
2385 138.25 5.23 Russell Wilson SEA QB 25.67 0.67 8700
Kareem Hunt CLE RB 18.96 0.18 8200
Jamaal Williams GBP RB 15.91 0.15 7000
Giovani Bernard CIN RB 14.47 0.26 5900
Tyler Lockett SEA WR 15.84 1.24 7200
Brandon Aiyuk SFO WR 11.98 0.41 5900
Nelson Agholor LVR WR 11.24 1.16 5000
Travis Kelce KCC TE 13.99 0.22 7900
Pittsburgh Steelers PIT DST 10.19 0.95 4200
7 138.54 6.09 Russell Wilson SEA QB 25.18 0.67 8700
Kareem Hunt CLE RB 18.90 0.18 8200
Jamaal Williams GBP RB 15.75 0.15 7000
Giovani Bernard CIN RB 14.30 0.26 5900
Davante Adams GBP WR 18.25 0.51 9100
Cole Beasley BUF WR 11.24 0.90 5300
Anthony Miller CHI WR 13.97 1.67 5100
Hunter Henry LAC TE 10.57 0.66 5600
Kansas City Chiefs KCC DST 10.38 1.08 5000
6786 141.26 6.27 Russell Wilson SEA QB 24.42 0.67 8700
Kareem Hunt CLE RB 18.95 0.18 8200
Jamaal Williams GBP RB 15.71 0.15 7000
Giovani Bernard CIN RB 14.47 0.26 5900
Davante Adams GBP WR 18.81 0.51 9100
Tyler Lockett SEA WR 18.91 1.24 7200
Justin Jefferson MIN WR 13.79 0.87 6600
Albert Okwuegbunam DEN TE 11.58 1.73 4700
New York Jets NYJ DST 4.63 0.67 2500
1038 143.38 10.59 Jarrett Stidham NEP QB 32.17 7.34 6500
Kareem Hunt CLE RB 18.78 0.18 8200
Jamaal Williams GBP RB 15.74 0.15 7000
Giovani Bernard CIN RB 13.95 0.26 5900
Davante Adams GBP WR 17.97 0.51 9100
Keenan Allen LAC WR 14.69 0.66 7400
Tyler Boyd CIN WR 12.65 0.06 6400
Darren Waller LVR TE 12.62 0.77 6800
New York Jets NYJ DST 4.81 0.67 2500
4177 144.50 11.14 Jarrett Stidham NEP QB 32.01 7.34 6500
Kareem Hunt CLE RB 19.03 0.18 8200
Giovani Bernard CIN RB 14.17 0.26 5900
David Montgomery CHI RB 12.23 0.10 5800
Davante Adams GBP WR 18.04 0.51 9100
Tyreek Hill KCC WR 16.08 0.62 8100
Keenan Allen LAC WR 16.19 0.66 7400
Hunter Henry LAC TE 10.45 0.66 5600
Detroit Lions DET DST 6.30 0.82 3400
8654 145.53 11.46 Jarrett Stidham NEP QB 31.61 7.34 6500
Kareem Hunt CLE RB 19.03 0.18 8200
Jamaal Williams GBP RB 15.97 0.15 7000
Giovani Bernard CIN RB 14.29 0.26 5900
Davante Adams GBP WR 17.64 0.51 9100
Justin Jefferson MIN WR 14.18 0.87 6600
Brandon Aiyuk SFO WR 12.06 0.41 5900
Hunter Henry LAC TE 10.20 0.66 5600
Kansas City Chiefs KCC DST 10.53 1.08 5000
9705 147.22 12.00 Brett Rypien DEN QB 35.27 8.89 6100
Derrick Henry TEN RB 20.29 0.14 9500
Kareem Hunt CLE RB 18.94 0.18 8200
Giovani Bernard CIN RB 13.43 0.26 5900
Tyler Boyd CIN WR 12.69 0.06 6400
Marquise Brown BAL WR 11.86 0.47 5900
Tre’Quan Smith NOS WR 9.82 0.16 5300
George Kittle SFO TE 14.50 0.76 7700
Kansas City Chiefs KCC DST 10.41 1.08 5000
630 147.80 12.59 Brett Rypien DEN QB 34.21 8.89 6100
Alvin Kamara NOS RB 19.77 0.75 9000
Kareem Hunt CLE RB 18.69 0.18 8200
Giovani Bernard CIN RB 14.22 0.26 5900
Tyreek Hill KCC WR 16.44 0.62 8100
Tyler Boyd CIN WR 12.71 0.06 6400
Brandon Aiyuk SFO WR 12.40 0.41 5900
George Kittle SFO TE 14.53 0.76 7700
New York Jets NYJ DST 4.82 0.67 2500
1774 149.08 13.22 Brett Rypien DEN QB 33.40 8.89 6100
Kareem Hunt CLE RB 18.85 0.18 8200
Jamaal Williams GBP RB 15.83 0.15 7000
Giovani Bernard CIN RB 14.40 0.26 5900
Davante Adams GBP WR 18.20 0.51 9100
Tyler Lockett SEA WR 16.46 1.24 7200
Tyler Boyd CIN WR 12.70 0.06 6400
Darren Waller LVR TE 12.78 0.77 6800
Cincinnati Bengals CIN DST 6.46 1.17 3300
4515 149.37 13.59 Brett Rypien DEN QB 33.46 8.89 6100
Kareem Hunt CLE RB 18.89 0.18 8200
Giovani Bernard CIN RB 14.22 0.26 5900
Myles Gaskin MIA RB 14.44 1.44 5700
Davante Adams GBP WR 18.15 0.51 9100
Tyler Boyd CIN WR 12.64 0.06 6400
Brandon Aiyuk SFO WR 11.83 0.41 5900
George Kittle SFO TE 14.85 0.76 7700
Kansas City Chiefs KCC DST 10.89 1.08 5000
1752 149.93 13.63 Brett Rypien DEN QB 35.93 8.89 6100
Alvin Kamara NOS RB 20.57 0.75 9000
Kareem Hunt CLE RB 18.68 0.18 8200
Giovani Bernard CIN RB 14.18 0.26 5900
Keenan Allen LAC WR 15.05 0.66 7400
Tyler Lockett SEA WR 15.70 1.24 7200
Tyler Boyd CIN WR 12.60 0.06 6400
Hunter Henry LAC TE 8.93 0.66 5600
Pittsburgh Steelers PIT DST 8.29 0.95 4200
3231 151.32 13.94 Brett Rypien DEN QB 37.46 8.89 6100
Alvin Kamara NOS RB 19.62 0.75 9000
Kareem Hunt CLE RB 18.76 0.18 8200
Jamaal Williams GBP RB 16.02 0.15 7000
Davante Adams GBP WR 17.26 0.51 9100
Tyler Lockett SEA WR 16.75 1.24 7200
Cole Beasley BUF WR 10.58 0.90 5300
Hunter Henry LAC TE 10.14 0.66 5600
New York Jets NYJ DST 4.74 0.67 2500
1272 151.65 14.51 Brett Rypien DEN QB 35.97 8.89 6100
Alvin Kamara NOS RB 19.73 0.75 9000
Kareem Hunt CLE RB 18.93 0.18 8200
Giovani Bernard CIN RB 14.35 0.26 5900
Tyreek Hill KCC WR 15.48 0.62 8100
Tyler Lockett SEA WR 16.42 1.24 7200
Nelson Agholor LVR WR 11.21 1.16 5000
George Kittle SFO TE 14.39 0.76 7700
New York Jets NYJ DST 5.16 0.67 2500
9666 152.55 14.63 Brett Rypien DEN QB 34.91 8.89 6100
Kareem Hunt CLE RB 18.97 0.18 8200
Giovani Bernard CIN RB 14.26 0.26 5900
Myles Gaskin MIA RB 13.39 1.44 5700
Davante Adams GBP WR 18.87 0.51 9100
Tyler Lockett SEA WR 17.07 1.24 7200
Brandon Aiyuk SFO WR 12.24 0.41 5900
George Kittle SFO TE 13.59 0.76 7700
Pittsburgh Steelers PIT DST 9.24 0.95 4200
656 155.23 14.71 Brett Rypien DEN QB 38.84 8.89 6100
Alvin Kamara NOS RB 20.10 0.75 9000
Kareem Hunt CLE RB 19.09 0.18 8200
Jamaal Williams GBP RB 15.77 0.15 7000
Davante Adams GBP WR 18.73 0.51 9100
Tyler Lockett SEA WR 16.72 1.24 7200
Anthony Miller CHI WR 10.75 1.67 5100
Hunter Henry LAC TE 10.25 0.66 5600
New York Jets NYJ DST 4.98 0.67 2500